package com.syh.jn.scm.infrastructure.repository.impl.downstream.system;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.downstream.entity.system.DownMenu;
import com.syh.jn.scm.domain.downstream.repository.system.DownMenuRepository;
import com.syh.jn.scm.dto.downstream.system.DownMenuVo;
import com.syh.jn.scm.infrastructure.repository.model.system.MenuModel;
import com.syh.jn.scm.infrastructure.repository.model.system.MenuPermissionModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DownMenuRepositoryImpl extends AbstractAggregateRepository<DownMenu, MenuModel, Long> implements DownMenuRepository {

  @Override
  public List<DownMenu> findListPage(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "DOWNSTREAM"));
    return super.findListPage(wrapper, startRow, count);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "DOWNSTREAM"));
    return super.getCount(wrapper);
  }

  @Override
  public <T> List<T> findListPageView(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "DOWNSTREAM"));
    return super.findListPageView(wrapper, startRow, count);
  }

  @Override
  public int getCountView(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "DOWNSTREAM"));
    return super.getCountView(wrapper);
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(DownMenuVo.class, "menu1", null, new String[]{"superName"}),
              "menu2.NAME as superName"
      );
      FROM("T_SYSTEM_MENU menu1");
      LEFT_OUTER_JOIN("T_SYSTEM_MENU menu2 ON menu1.SUPER_ID = menu2.ID ");
    }};
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String, String> params = new HashMap<>();
    params.put("terminalType", "menu1.TERMINAL_TYPE");
    params.put("type", "menu1.TYPE");
    params.put("orderLevel", "menu1.ORDER_LEVEL");
    params.put("name", "menu1.NAME");
    params.put("id", "menu1.ID");
    params.put("catalogCode", "menu1.CATALOG_CODE");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }

  @Override
  protected Class getViewObject() {
    return DownMenuVo.class;
  }

  @Override
  public void updateMenuPermission(MenuPermissionModel munePerm) {
    this.hibernateTemplate.saveOrUpdate(munePerm);
  }

  @Override
  public void removeMenuPermissionByIds(Long menuId, List<Long> menuPermsDeleteIds) {
    String hql = "delete from MenuPermissionModel where menuId = :menuId and permissionId in(:permissionIds)";
    Query query = this.buildQuery(hql);
    query.setLong("menuId", menuId);
    query.setParameterList("permissionIds", menuPermsDeleteIds);
    query.executeUpdate();
  }

}
